package com.poh.controller;

import com.poh.Utils.Timer;
import com.poh.pojo.Course;
import com.poh.pojo.SU;
import com.poh.pojo.SingIn;
import com.poh.pojo.User;
import com.poh.service.CourseService;
import com.poh.service.SUService;
import com.poh.service.SingInService;
import com.poh.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
public class SingInController {
    @Autowired
    SingInService singInService;
    @Autowired
    SUService suService;
    @Autowired
    CourseService courseService;
    @Autowired
    UserService userService;

    @GetMapping("/toSingIn")
    public String toSingIn(HttpSession session, Model model) {
        Course course = (Course) session.getAttribute("course");
        List<SingIn> singIns = singInService.querySingInByCourseId(course.getCourseId());
        for (SingIn singIn : singIns) {
            if (singIn.getSingInStatus() == 0) {
                if (Timer.judegeTime(singIn.getAddTime(), singIn.getSTime()) == 1) {
                    break;
                } else if (Timer.judegeTime(singIn.getAddTime(), singIn.getSTime()) == 2) {
                    singInService.updateSUSingInStatus(singIn.getSId(), Timer.judegeTime(singIn.getAddTime(), singIn.getSTime()));
                }
            }
        }
        model.addAttribute("singIns", singIns);
        return "signIn/teacherSingIn";
    }

    @PostMapping(value = "/addSingIn", params = "action=save")
    public String saveSingIn(SingIn singIn) {
        String sid = UUID.randomUUID().toString();
        singIn.setSId(sid);
        singIn.setSingInStatus(0);
        singInService.addSingIn(singIn);
        return "redirect:/toSingIn";
    }
    @PostMapping(value = "/addSingIn", params = "action=start")
    public String startSingIn(SingIn singIn, HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        String sid = UUID.randomUUID().toString();
        singIn.setSId(sid);
        singIn.setSingInStatus(1);
        SU su = new SU();
        su.setSId(singIn.getSId());
        su.setSingInTime(singIn.getAddTime());
        su.setCourseId(course.getCourseId());
        su.setSId(singIn.getSId());
        List<User> students = userService.getStudentByCourseId(course.getCourseId());
        if (students != null) {
            for (User user : students) {
                su.setUserId(user.getUserId());
                suService.addSU(su);
            }
        }
        singInService.addSingIn(singIn);
        return "redirect:/toSingIn";
    }
    @GetMapping("/startSingIn/{sId}")
    public String startSingIn(@PathVariable("sId") String sId, HttpSession session) {
        SingIn singIn = singInService.querySingInBySid(sId);
        Course course = (Course) session.getAttribute("course");
        singInService.updateSingInStatus(1, sId);
        List<User> students = userService.getStudentByCourseId(course.getCourseId());
        SU su = new SU();
        su.setSId(singIn.getSId());
        su.setSingInTime(singIn.getAddTime());
        su.setCourseId(course.getCourseId());
        su.setSId(singIn.getSId());
        if (students != null) {
            for (User user : students) {
                su.setUserId(user.getUserId());
                suService.addSU(su);
            }
        }
        return "redirect:/toSingIn";
    }
    @GetMapping("/endSingIn/{sId}")
    public String endSingIn(@PathVariable("sId") String sId) {
        singInService.updateSingInStatus(2, sId);
        return "redirect:/toSingIn";
    }
    @GetMapping("/deleteSingIn/{sId}")
    public String deleteSingIn(@PathVariable("sId") String sId) {
        singInService.deleteSingIn(sId);
        return "redirect:/toSingIn";
    }


    @GetMapping("/toStudentSingIn")
    public String toStudentSingIn(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        Course course = (Course) session.getAttribute("course");
        List<SU> sus = suService.querySUByUserIdAndCourseId(user.getUserId(), course.getCourseId());
        for (SU su : sus) {
            su.getSingIn().getSTime();
            if (Timer.judegeTime(su.getSingIn().getAddTime(), su.getSingIn().getSTime()) == 1) {
                break;
            } else if (Timer.judegeTime(su.getSingIn().getAddTime(), su.getSingIn().getSTime()) == 2) {
                singInService.updateSUSingInStatus(su.getSId(), Timer.judegeTime(su.getSingIn().getAddTime(), su.getSingIn().getSTime()));
            }
        }
        model.addAttribute("countSingingIn", suService.getCountMySingIn(user.getUserId(), course.getCourseId(), 1));
        model.addAttribute("countSingedIn", suService.getCountMySingIn(user.getUserId(), course.getCourseId(), 2));
        model.addAttribute("sus", sus);
        return "signIn/studentSingIn";
    }
    @GetMapping("/studtentsingInInfo/{sId}")
    public String studtentsingInInfo(@PathVariable("sId") String sId, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        SU su = suService.querySUBySIdAndUserId(sId, user.getUserId());
        SingIn singIn = singInService.querySingInBySid(sId);
        if (Timer.judegeTime(singIn.getAddTime(), singIn.getSTime()) == 2) {
            singInService.updateSUSingInStatus(singIn.getSId(), Timer.judegeTime(singIn.getAddTime(), singIn.getSTime()));
        }
        model.addAttribute("su", su);
        return "signIn/studtentsingInInfo";
    }
    @GetMapping("/studentSingIn/{sId}")
    public String studentSingIn(@PathVariable("sId") String sId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        suService.updateSUStudentSingInStatus(sId, user.getUserId(),1);
        return "redirect:/studtentsingInInfo/" + sId;
    }
    @PostMapping("/codeSingIn")
    public String codeSingIn(SingIn singIn, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        String code = singInService.queryCodeBySid(singIn.getSId());
        model.addAttribute("msg", "");
        if (singIn.getSingInCode().equals(code)) {
            suService.updateSUStudentSingInStatus(singIn.getSId(), user.getUserId(),1);
        } else {
            model.addAttribute("msg", "签到码错误！");
        }
        return "redirect:/studtentsingInInfo/" + singIn.getSId();
    }

    @GetMapping("/teaSingInInfo/{sId}")
    public String teaSingInInfo(@PathVariable("sId") String sId, Model model) {
        SingIn singIn = singInService.querySingInBySid(sId);
        List<SU> sus = suService.querySingInStuBySid(sId);
        model.addAttribute("singInStu", suService.getCountSingIn(sId, 1));
        model.addAttribute("notSingInStu", suService.getCountSingIn(sId, 0));
        model.addAttribute("sus", sus);
        model.addAttribute("singIn", singIn);
        return "/signIn/teacherSingInInfo";
    }

}
